package com.relman.command
{

	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.commands.ICommand;

	import com.relman.model.RelmanModelLocator;
	import com.relman.business.UserDelegate;
	import com.relman.control.LoginEvent;
	import com.relman.vo.UserVO;
	import com.relman.util.DebugUtils;

	import mx.controls.Alert;
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;
	

	public class LoginCommand implements ICommand, IResponder
	{
		public function execute(event:CairngormEvent):void
		{
		    var delegate : UserDelegate = new UserDelegate( this );
		    delegate.login(event.data)
		}
		
		public function result(data:Object):void
		{
			var model : RelmanModelLocator = RelmanModelLocator.getInstance();
			var response_xml : XML = data.result.children();
			DebugUtils.debug("checking what response xml looks like");
			DebugUtils.debug(response_xml.toString());
			if (response_xml.user) {
				model.currentUser = UserVO.fromXML(response_xml.user.toXMLString()); 
			} else {
				model.currentUser = null;
			}
		}
		
		public function fault(info:Object):void
		{
			var faultEvent :FaultEvent = FaultEvent( info )
			Alert.show( "fault at login" )
		}
		
	}
}
